<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class manage extends admin {
	private $db;
	public $site;
	function __construct(){
		parent::__construct();
		pc_base::load_app_func('global');
		$this->db = pc_base::load_model('course_model');//加载model
		$this->siteid = get_siteid();//获取当前站点
		$this->admin_username = param::get_cookie('admin_username');//获取当前登录的管理员
	}
	
	/**
	 *课程信息列表
	 */
	public function init() {
		$where = "";
		$where .= "status = 0 and site =".$this->siteid;
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$courseinfo = $this->db->listinfo($where,'id DESC',$page,10);
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('course_list');
	}
	
	/**
	 *添加课程
	 */
	public function add() {
		if (isset($_POST['dosubmit'])) {
			$info = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
			$data['name'] = trim($info['name']);
			$data['description'] = $info['description'];
			$data['thumb'] = $info['thumb'];
			$data['content'] = $info['content'];
			$data['course_time'] = strtotime($info['time']);
			$data['username'] = $this->admin_username;
			$data['inputtime'] = $data['updatetime'] = time();
			$data['userid'] = $_SESSION['userid'];
			$data['site'] = $this->siteid;
			$data['status'] = 0;
			$result = $this->db->insert($data);
			if ($result) {
				showmessage(L('add_success'),'?m=course&c=manage&a=init');
			} else {
				showmessage(L('add_error'),HTTP_REFERER);
			}
		} else {
			$show_dialog = $show_validator = true;
			pc_base::load_sys_class('format', '', 0);
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('course_add');
		}
	}
	
	/**
	 *编辑课程
	 */
	public function edit() {
		$id = isset($_GET['id']) ? $_GET['id'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
		$upinfo = $_POST['data'];
		if (isset($_POST['dosubmit'])) {
			$data['name'] = trim($upinfo['name']);
			$data['description'] = $upinfo['description'];
			$data['thumb'] = $upinfo['thumb'];
			$data['content'] = $upinfo['content'];
			$data['course_time'] = strtotime($upinfo['time']);
			$data['username'] = $this->admin_username;
			$data['updatetime'] = time();
			$data['userid'] = $_SESSION['userid'];
			$result = $this->db->update($data,array('id'=>$id));
			if ($result) {
				showmessage(L('edit_success'),'?m=course&c=manage&a=init');
			} else {
				showmessage(L('edit_error'),HTTP_REFERER);
			}
		} else {
			$show_dialog = $show_validator = true;
			$show_header = '';
			pc_base::load_sys_class('format', '', 0);
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('course_edit');
		}
	}
	
	/**
	 *课程信息删除、批量删除
	 */
	public function del() {
		if (!isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k=>$v){
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$result = $this->db->update(array('status'=>'1'),"id in ('$id')");
		if($result){
			showmessage(L('del_success'),HTTP_REFERER);
		}else {
			showmessage(L('del_error'),HTTP_REFERER);
		}
	}
}